﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using CL.Business.Entity;
using CL.Dapper.Repository;
using CL.Enum.Common;
using CL.View.Entity.Business;
using Dapper;
using CL.Tools.Common;

namespace CL.Business.DAL
{
    public class RebateDAL : DataRepositoryBase<RebateEntity>
    {
        public RebateDAL(DbConnectionEnum conenum, IDbConnection Db = null) : base(conenum, Db)
        {
        }
        /// <summary>
        /// 查询对象集合
        /// </summary>
        /// <param name="UnionID"></param>
        /// <returns></returns>
        public List<RebateEntity> QueryEntitys(int AccountID)
        {
            return base.GetList(new { AccountID = AccountID }, "ModifyTime desc").ToList();
        }
        /// <summary>
        /// 查询单个对象
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public RebateEntity QueryEntity(int ID)
        {
            return base.Get(ID);
        }
        /// <summary>
        /// 新增对象
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public int InsertEntity(RebateEntity Entity)
        {
            return base.Insert(Entity) ?? 0;
        }
        /// <summary>
        /// 更新对象
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public int ModifyEntity(RebateEntity Entity)
        {
            return base.Update(Entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelEntity(int id)
        {
            return base.Delete(id) > 0;
        }


        /// <summary>
        /// 代理返点设置数据是否存在
        /// </summary>
        /// <param name="navname"></param>
        /// <returns></returns>
        public bool AccountExists(int id,long amount)
        {
            return base.RecordCount(new { AccountID = id, Amount = amount }) == 0 ? false : true;
        }

        /// <summary>
        /// 返点设置数据是否存在
        /// </summary>
        /// <param name="navname"></param>
        /// <returns></returns>
        public bool Exists(int id)
        {
            return base.RecordCount(new { RebateID = id }) == 0 ? false : true;
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        /// <param name="GameName"></param>
        /// <returns></returns>
        public List<udv_CommissionRebateViewModel> QueryListByPage(int pageSize, int pageIndex, string searchKey, ref int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            string sqlParam = "";
            var para = new DynamicParameters();
            if(!string.IsNullOrEmpty(searchKey))
            {
                searchKey = "%" + searchKey + "%";
                para.Add("@AccountCode", searchKey);
                para.Add("@UserMobile", searchKey);
                para.Add("@FullName", searchKey);
                sqlParam = " AND (b.AccountCode LIKE @AccountCode OR b.FullName LIKE @FullName OR b.UserMobile LIKE @UserMobile)";
            }
            strSql.Append(@"SELECT a.*, b.AccountCode FROM dbo.UN_Rebate a JOIN dbo.UN_Account b ON a.AccountID = b.AccountID WHERE a.Amount>=0 " + sqlParam);
            recordCount = base.GetIntSingle(PagingHelper.CreateCountingSql(strSql.ToString()), para);
            if (recordCount == 0)
                return null;

            SqlMapper.GridReader grid = base.QueryMultiple(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), " RebateID ASC "), para);
            List<udv_CommissionRebateViewModel> list = grid.Read<udv_CommissionRebateViewModel>().ToList();
            grid.Dispose();
            return list;
        }
    }
}
